import Toast from '@vant/weapp/toast/toast';
var reqHelp = require('../../utils/req.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        idCardUrl: '',
        btnShow: false,
        ctx: null,
        duration: 0,
        timer: null,
        group: 'front',
        cardId: 0,
        userId: 0,
        realNameGuid: '0'
    },
    /**
     * 保存
     */
    savePhoto() {
        var that = this;
        Toast.loading({
            duration: 0, 
            message: '上传中..',
        });
        const {
            idCardUrl,
            group,
            cardId,
            userId,
            realNameGuid
        } = this.data;
        // console.log('0' + group + '/' + cardId + '/' + userId + '/' + realNameGuid + '/' + idCardUrl);]
        if (group == 'front') {
            reqHelp.requestByUpload('Mini/RealName.ashx?action=IdCardFront', {
                cardId: cardId,
                userId: userId
            }, idCardUrl).then(function (result) {
                Toast.clear();
                var res = JSON.parse(result);
                if (res.getNum == 0) {
                    that.setData({
                        frontImg: idCardUrl,
                        realNameGuid: res.realNameGuid
                    })
                    Toast({
                        type: 'success',
                        message: '上传成功',
                        duration: 3000,
                        onClose: () => {
                            wx.redirectTo({
                                url: 'uploadImg?frontImg=' + idCardUrl + '&cardId=' + cardId + '&userId=' + userId + '&realNameGuid=' + res.realNameGuid,
                            })
                        },
                    });
                } else {
                    Toast({
                        type: 'text',
                        message: res.getMsg,
                        duration: 3500,
                    });
                }
            });
        } else {
            reqHelp.requestByUpload('Mini/RealName.ashx?action=IdCardBack', {
                realNameGuid: realNameGuid,
            }, idCardUrl).then(function (result) {
                Toast.clear();
                var res = JSON.parse(result);
                if (res.getNum == 0) {
                    Toast({
                        type: 'success',
                        message: '上传成功',
                        duration: 3000,
                        onClose: () => {
                            wx.redirectTo({
                                url: 'uploadVideo?realNameGuid=' + realNameGuid,
                            })
                        },
                    });
                } else {
                    Toast({
                        type: 'text',
                        message: res.getMsg,
                        duration: 3500,
                    });
                }
            });
        }
    },

    /**
     * 拍照
     */
    takeingPhoto() {
        const that = this
        this.data.ctx.takePhoto({
            quality: 'normal',
            success: (res) => {
                console.log(res)
                that.setData({
                    idCardUrl: res.tempImagePath,
                    btnShow: true
                })
            },
            fail: (err) => {
                console.log(err)
            }
        })
    },
    onLoad(options) {
        this.setData({
            group: options.group,
            cardId: options.cardId,
            userId: options.userId,
            realNameGuid: options.realNameGuid,
            ctx: wx.createCameraContext()
        });
        this.listen()
    },
    listen() {
        const context = this.data.ctx
        const listener = context.onCameraFrame((frame) => {
            // console.log(frame.data instanceof ArrayBuffer, frame.width, frame.height)
        })
        this.setData({
            listener: listener
        })
    },
    /**
     * 取消
     */
    cancelPhoto() {
        this.setData({
            btnShow: false,
            idCardUrl: ''
        })
    }
})